import http from '@/utils/http';

export async function website_list() {
  return await http.get('/script/list');
}

export async function script_execute(script_id, data = []) {
  if (!script_id) throw new Error('script_id is empty | got ' + script_id);
  console.log(`output->running: script_id`, script_id);
  return await http.post(`/script/execute?scriptId=${script_id}`, data);
}

export async function script_update(data) {
  return await http.put('/script', data);
}

export const execute = <T>(id: string | number, data: Obj) => {
  const params = Object.entries(data).map(([key, value]) => {
    let type: string;
    switch (typeof value) {
      case 'string':
        type = 'string';
        break;
      case 'number':
        type = 'number';
        break;
      default:
        type = '';
    }
    return {
      paramId: null,
      name: key,
      dataType: type,
      paramType: 1,
      value,
    };
  });
  return post<T>(Prefix.api + `/script/execute?scriptId=${id}`, params);
};
